package com.play.ballen.synthetic.ui

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.Button
import androidx.core.content.ContextCompat
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivitySyntheticBinding
import com.play.ballen.event.EventCost
import com.play.ballen.home.task.TaskUtil
import com.play.ballen.me.dialog.OperateSuccessDialogUtils
import com.play.ballen.pack.entity.PackBallInfo
import com.play.ballen.synthetic.dialog.GuessDialogUtils
import com.play.ballen.synthetic.dialog.SyntheticDialogUtils
import com.play.ballen.synthetic.entity.SyntheticInfo
import com.play.ballen.synthetic.vm.SyntheticVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.imageloader.ImageUtil
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.component.eventbus.Event
import com.yuven.baselib.component.eventbus.EventBusUtil
import com.yuven.baselib.utils.ResourceUtil

class SyntheticActivity : BaseStdTitleActivity<SyntheticVM, ActivitySyntheticBinding>() {

    var id = ""

    var id1 = ""
    var id2 = ""
    var id3 = ""

    private var info: SyntheticInfo? = null
    var clickChangeBallId = 0

    companion object {
        @JvmStatic
        fun start(context: Context, id: String, info: SyntheticInfo) {
            val starter = Intent(context, SyntheticActivity::class.java)
            starter.putExtra("id", id)
            starter.putExtra("info", info)
            context.startActivity(starter)
        }
    }

    private val operateSuccessDialogUtils by lazy { OperateSuccessDialogUtils(this) }
    private val syntheticDialogUtils by lazy {
        SyntheticDialogUtils(this).apply {
            valueCallback = { select: PackBallInfo ->
                when (clickChangeBallId) {
                    R.id.imgBall1 -> {
                        id1 = select.id
                        ImageUtil.instance.loadImage(0, 0, select.pic, dataBinding.imgBall1)
                        dataBinding.imgAddBall1.visibility= (View.GONE)
                    }

                    R.id.imgBall2 -> {
                        id2 = select.id
                        ImageUtil.instance.loadImage(0, 0, select.pic, dataBinding.imgBall2)
                        dataBinding.imgAddBall2.visibility= (View.GONE)
                    }

                    R.id.imgBall3 -> {
                        id3 = select.id
                        ImageUtil.instance.loadImage(0, 0, select.pic, dataBinding.imgBall3)
                        dataBinding.imgAddBall3.visibility= (View.GONE)
                    }
                }

                var count = 0;
                if (id1.isNotEmpty()) {
                    count++
                }
                if (id2.isNotEmpty()) {
                    count++
                }
                if (id3.isNotEmpty()) {
                    count++
                }
                dataBinding.tvNumberSelect.text = "$count "
            }
        }
    }

    override fun viewModelClass(): Class<SyntheticVM> {
        return SyntheticVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_synthetic
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.listSyntheticChooseBall.observe(this) {
            if (it.isNullOrEmpty()) {
                return@observe
            }
            showSyntheticBallChooseDialog(it)
        }

        viewModel.actionSyntheticSuccess.observe(this) {
            operateSuccessDialogUtils.show(ResourceUtil.getString(R.string.synthetic_success_tip))
            EventBusUtil.post(Event.create(EventCost.CODE_SYNTHETIC_SUCCESS))
            TaskUtil.resetStepCount()
        }
    }

    private val onClickListener = { v: View ->
        changeBall(v)
    }

    override fun initData() {
        info?.run {
            dataBinding.tvCondition.text =
                ResourceUtil.getString(R.string.synthetic_condition, num, name)
            dataBinding.tvServiceFee.text =
                ResourceUtil.getString(R.string.price_unit_bmt, fee)
        }

        dataBinding.imgBall1.setOnSingleClickListener(onClickListener)
        dataBinding.imgBall2.setOnSingleClickListener(onClickListener)
        dataBinding.imgBall3.setOnSingleClickListener(onClickListener)

        dataBinding.btnConfirm.setOnSingleClickListener tag@{
            if (id1.isEmpty() || id2.isEmpty() || id3.isEmpty()) {
                ToastUtils.show(ResourceUtil.getString(R.string.synthetic_btn_empty_ball_tip))
                return@tag
            }
            viewModel.syntheticSubmit(id, "$id1,$id2,$id3")
        }
    }

    override fun initIntent(intent: Intent?) {
        intent?.let {
            id = intent.getStringExtra("id") ?: ""
            info = intent.getSerializableExtra("info") as? SyntheticInfo
        }
    }

    override fun getToolBarTitle(): String = ResourceUtil.getString(R.string.synthetic_btn_text)

    private fun changeBall(v: View) {
        clickChangeBallId = v.id

        val list = viewModel.listSyntheticChooseBall.value
        if (!list.isNullOrEmpty()) {
            showSyntheticBallChooseDialog(list)
        } else {
            viewModel.memberBasketBall()
        }

        if (!id1.isEmpty() && !id2.isEmpty() && !id3.isEmpty()) {
            //dataBinding.btnConfirm.background = "@drawable/layer_orange_shadow_button"
            val a = findViewById<Button>(R.id.btnConfirm)
            a.background = ContextCompat.getDrawable( mContext, R.drawable.layer_orange_shadow_button)
        }
    }

    private fun showSyntheticBallChooseDialog(items: List<PackBallInfo>?) {
        syntheticDialogUtils.show(items, id1, id2, id3)
    }
}